﻿using CMS.DAL;
using CMS.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CMS.BLL
{
    public class BizPost
    {
        /// <summary>
        /// Tổng số Post
        /// </summary>
        public static int TotalPost
        {
            get
            {
                using (var db = new DBContext())
                {
                    return db.Posts.Count();
                }
            }
            set
            {
                throw new Exception("Thuộc tính chỉ đọc");
            }
        }
        /// <summary>
        /// Tổng số Post theo blog
        /// </summary>
        /// <param name="idBlog"></param>
        /// <returns></returns>
        public static int TotalPostByBlog(string idBlog)
        {
            using (var db = new DBContext())
            {
                return (from e in db.Posts
                        where e.BlogID == idBlog
                        select e).Count();
            }
        }

        public static bool Add(string title, string content, string idBlog, string idUser, bool isShow, string tags)
        {
            using (var db = new DBContext())
            {
                Post post = new Post
                {
                    ID = Guid.NewGuid().ToString(),
                    Title = title,
                    Content = content,
                    Show = isShow,
                    DateCreate = DateTime.Now,
                    BlogID = idBlog,
                    UserID = idUser,
                    Tags = tags
                };
                db.Posts.Add(post);
                db.SaveChanges();
                return true;
            }
        }
        public static bool Update(Post _post)
        {
            using (var db = new DBContext())
            {
                var post = (from e in db.Posts
                            where e.ID == _post.ID
                            select e).SingleOrDefault();
                if (post == null)
                    return false;
                post.Title = _post.Title;
                post.Content = _post.Content;
                post.Show = _post.Show;
                post.BlogID = _post.BlogID;
                post.LastDateModify = DateTime.Now;
                post.LastUserID = _post.LastUserID;
                post.Tags = _post.Tags;
                db.SaveChanges();
                return true;
            }
        }
        public static bool Delete(Post _post)
        {
            using (var db = new DBContext())
            {
                var post = (from e in db.Posts
                            where e.ID == _post.ID
                            select e).SingleOrDefault();
                if (post == null)
                    return false;
                if (post.Comments.Count > 0)
                    return false;
                db.Posts.Remove(post);
                return true;
            }
        }

        /// <summary>
        /// Lấy danh sách bài viết theo Blog
        /// </summary>
        /// <param name="idBlog">Mã Blog</param>
        /// <param name="orderPage">Số thứ tự trang</param>
        /// <param name="postPerPage">Số bài viết trong mỗi page</param>
        /// <returns></returns>
        public static List<Post> GetList(string idBlog = null, int orderPage = 0)
        {
            using (var db = new DBContext())
            {
                List<Post> ls = null;
                if (string.IsNullOrEmpty(idBlog))
                {
                    if (orderPage == 0)
                    {
                        //Lấy hết tất cả các bài viết
                        ls = (from e in db.Posts
                              orderby e.DateCreate descending
                              select e).ToList();
                    }
                    else
                    {
                        //Lấy số post theo trang
                        ls = (from e in db.Posts
                              orderby e.DateCreate descending
                              select e).Skip((orderPage - 1) * Config.PostPerPage).Take(Config.PostPerPage).ToList();
                    }
                }
                else
                {
                    if (orderPage == 0)
                    {
                        //Lấy hết tất cả các bài viết theo blog
                        ls = (from e in db.Posts
                              where e.BlogID == idBlog
                              orderby e.DateCreate descending
                              select e).ToList();
                    }
                    else
                    {
                        //Lấy số post theo trang của môt blog
                        ls = (from e in db.Posts
                              where e.BlogID == idBlog
                              orderby e.DateCreate descending
                              select e).Skip((orderPage - 1) * Config.PostPerPage).Take(Config.PostPerPage).ToList();
                    }
                }
                return null;
            }
        }

        public static List<Post> GetFeature()
        {
            using (var db = new DBContext())
            {
                var lsFeature = (from e in db.Posts
                                 where e.Feature == true
                                 orderby e.Comments.Count descending
                                 select e).ToList();
                if (lsFeature == null || lsFeature.Count < 10)
                {
                    var lsDate = (from e in db.Posts
                                  orderby e.DateCreate descending
                                  select e).ToList();
                    if (lsDate != null)
                        lsFeature.AddRange(lsDate);
                }
                return lsFeature;
            }
        }
    }
}